#include "klippy/core.h"
#include "klippy/config.h"
#include <gtest/gtest.h>
#include <fstream>

class ConfigTest : public ::testing::Test {
protected:
    void SetUp() override {
        // 创建键值对测试配置文件
        std::ofstream kv_file("test_kv.conf");
        kv_file << "printer.name = TestPrinter\n";
        kv_file << "motion.velocity = 100\n";
        kv_file << "temperature.bed = 60\n";
        kv_file.close();

        // 创建JSON测试配置文件
        std::ofstream json_file("test_json.conf");
        json_file << R"({
            "printer": {
                "name": "TestPrinter"
            },
            "motion": {
                "velocity": 100
            },
            "temperature": {
                "bed": 60
            }
        })";
        json_file.close();
    }

    void TearDown() override {
        remove("test_kv.conf");
        remove("test_json.conf");
    }
};

TEST_F(ConfigTest, KeyValueConfigLoad) {
    auto config = klippy::Config::load("test_kv.conf");
    ASSERT_NE(config, nullptr);
    EXPECT_EQ(config->get_string("printer.name"), "TestPrinter");
    EXPECT_EQ(config->get_int("motion.velocity"), 100);
    EXPECT_EQ(config->get_float("temperature.bed"), 60.0f);
}

TEST_F(ConfigTest, JsonConfigLoad) {
    auto config = klippy::Config::load("test_json.conf");
    ASSERT_NE(config, nullptr);
    EXPECT_EQ(config->get_string("printer.name"), "TestPrinter");
    EXPECT_EQ(config->get_int("motion.velocity"), 100);
    EXPECT_EQ(config->get_float("temperature.bed"), 60.0f);
}

TEST_F(ConfigTest, ConfigReload) {
    auto config = klippy::Config::load("test_kv.conf");
    ASSERT_NE(config, nullptr);
    EXPECT_TRUE(config->reload());
}

TEST_F(ConfigTest, InvalidConfig) {
    std::ofstream bad_file("bad.conf");
    bad_file << "invalid_config";
    bad_file.close();

    EXPECT_THROW(klippy::Config::load("bad.conf"), std::runtime_error);
    remove("bad.conf");
}
